package py.una.pol.tesis.ii.emergenciasmovil.app.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import py.una.pol.tesis.ii.emergenciasmovil.app.R;
import py.una.pol.tesis.ii.emergenciasmovil.app.clases.Pariente;

/**
 * Created by leandro on 9/11/2014.
 */
public class ParienteListAdapter extends BaseAdapter {
    protected Activity activity;
    private Context context;
    private ArrayList<Pariente> parienteArray;
    private List<Pariente> parienteList = null;
    private int pos;

    public ParienteListAdapter(Activity activity, ArrayList<Pariente> parientes) {
        this.activity = activity;
        this.parienteList = parientes;
        this.parienteArray = new ArrayList<Pariente>();
        this.parienteArray.addAll(parientes);
    }

    @Override
    public int getCount() {
        return parienteList.size();
    }

    @Override
    public Object getItem(int i) {
        return parienteList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup parent) {
        if (view == null) {
            LayoutInflater mInflater = (LayoutInflater)
                    activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            view = mInflater.inflate(R.layout.item_list_pariente, null);
        }
        this.pos=i;
        TextView nombrePariente = (TextView) view.findViewById(R.id.nombrePariente);
        nombrePariente.setText(parienteList.get(i).getNombres() + " " + parienteList.get(i).getApellidos());
        TextView movilPariente = (TextView) view.findViewById(R.id.movilPariente);
        movilPariente.setText(parienteList.get(i).getNumeroMovil());

        return view;
    }


    public void filtrar(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        parienteList.clear();
        if (charText.length() == 0) {
            parienteList.addAll(parienteArray);
        }
        else
        {
            for (Pariente pariente : parienteArray)
            {
                if ((pariente.getNombres() + pariente.getApellidos()).toLowerCase(Locale.getDefault()).contains(charText))
                {
                    parienteList.add(pariente);
                }
            }
        }
        notifyDataSetChanged();
    }

}
